package com.udf.flink.udf;

/*
 * @description：TODO
 * @author：jianglai
 * @date：2021/1/26 4:50 下午
 */
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.table.functions.ScalarFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @description：处理json字段
 * @author：jianglai
 * @date：2021/1/26 11:00 上午
 */
public class ReadJson extends ScalarFunction {

    private static final Logger LOG = LoggerFactory.getLogger(ReadJson.class);
    /**
     * @author jianglai
     * @Description: 替换\
     * @date 2021/1/26
     * @params [str]
     * @return java.lang.String
     */
    public static String repacelFunc(String str){
        return str.replace("\"", "\"");
    }

    /**
     * @author jianglai
     * @Description: 处理1层json
     * @date 2021/1/26
     * @params [json, key, key1]
     * @return java.lang.String
     */
    public static String readJson(String json, String key){
        String s = repacelFunc(json);
        String keys = "";
        try {
            keys = JSON.parseObject(s).getString(key);
        }catch (Exception e){
            LOG.error(e.getMessage());
        }
        return keys;
    }

    /**
     * @author jianglai
     * @Description: 处理2层json
     * @date 2021/1/26
     * @params [json, key, key1]
     * @return java.lang.String
     */
    public static String readJson(String json, String key,String key1){
        String s = repacelFunc(json);
        String keys = "";
        try {
            keys = JSON.parseObject(s).getJSONObject(key).getString(key1);
        }catch (Exception e){
            LOG.error(e.getMessage());
        }
        return keys;
    }

    /**
     * @author jianglai
     * @Description: 处理3层json
     * @date 2021/1/26
     * @params [json, key, key1, key2]
     * @return java.lang.String
     */
    public static String readJson(String json, String key,String key1,String key2){
        String s = repacelFunc(json);
        String keys = "";
        try {
            JSONObject secJson = JSON.parseObject(s);
            keys = secJson.getJSONObject(key).getJSONObject(key1).getString(key2);
        }catch (Exception e){
            LOG.error(e.getMessage());
        }
        return keys;
    }

    /**
     * @author jianglai
     * @Description: 处理1层json逻辑
     * @date 2021/1/26
     * @params [json, key]
     * @return java.lang.String
     */
    public String eval(String json,String key){
        return readJson(json, key);
    }

    /**
     * @author jianglai
     * @Description: 处理2层json逻辑
     * @date 2021/1/26
     * @params [json, key, key1]
     * @return java.lang.String
     */
    public String eval(String json,String key,String key1){
        return readJson(json, key,key1);
    }

    /**
     * @author jianglai
     * @Description: 处理3层json逻辑
     * @date 2021/1/26
     * @params [json, key, key1, key2]
     * @return java.lang.String
     */
    public String eval(String json,String key,String key1,String key2){
        return readJson(json, key, key1, key2);
    }
}

